package com.borko.healthcare.tapestry.pages;

import java.util.List;

import javax.inject.Inject;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.services.Cookies;
import org.springframework.context.ApplicationContext;

import com.borko.healthcare.entities.interfaces.Medicine;
import com.borko.healthcare.entities.interfaces.Supplier;
import com.borko.healthcare.persistence.hibernate.services.MedicineManager;
import com.borko.healthcare.persistence.hibernate.services.SupplierManager;

public class ViewMedicines {
	
	@Inject
	private Cookies cookies;
	
	@Inject
	private ApplicationContext applicationContext;
	
	private SupplierManager supplierManager;
	private MedicineManager medicineManager;
	
	@SuppressWarnings("unused")
	@Property
	private Medicine medicine;
	
	public List<Medicine> getMedicines() {
		supplierManager = (SupplierManager) applicationContext.getBean("supplierManager");
		
		int supplierId = Integer.parseInt(cookies.readCookieValue("userId"));
		Supplier supplier = supplierManager.getSupplier(supplierId);
		return supplier.getMedicines();
	}
	
	public void onActionFromDelete(int medicineId) {
		System.out.println(medicineId);
		medicineManager = (MedicineManager) applicationContext.getBean("medicineManager");
		Medicine medicineToRemove = medicineManager.getMedicine(medicineId);
		medicineManager.remove(medicineToRemove);
	}

}
